﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Linq;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace OttawaTourMap
{
    public class TourData : INotifyPropertyChanged
    {
       // string filename = @"TourData\TourListInfo.xml";
        /// <summary>
        /// A collection for Tour lists
        /// </summary>
        public ObservableCollection<Tour> Items { get; private set; }
        public TourData()
        {
            this.Items = new ObservableCollection<Tour>();
        }

        private bool isFileLoaded = false;
        public bool isTourFileLoaded()
        {
            return isFileLoaded;
        }
        
        public void parseXML()
        {
            try
            {
               // XElement toursList;
                //if (File.Exists(filename))
                //{
                //toursList = XElement.Load(filename);
                XDocument xdoc = XDocument.Load("TourData\\TourListInfo.xml");
                var toursListElement = xdoc.Elements("toursList");
                
                List<Tour> tours = (from tour in toursListElement.Elements("tour")
                                    select new Tour
                                    {
                                        language = tour.Attribute("language").Value,
                                        id = tour.Attribute("id").Value,
                                        //This Tag is optional
                                        thumbnailURI = tour.Element("ThumbNailURI")!=null?tour.Element("ThumbNailURI").Value:"",
                                        title = tour.Element("title").Value,
                                        description = tour.Element("description").Value,
                                        //This Tag is optional
                                        author = tour.Element("author")!=null?tour.Element("author").Value:"",

                                        tourStops = (from stop in tour.Elements("stop")
                                                     select new TourStop
                                                     {
                                                         id = stop.Attribute("id").Value,
                                                         title = stop.Element("title").Value,
                                                         description = stop.Element("description").Value,
                                                         //This tag is optional
                                                         address = stop.Element("address")!= null?stop.Element("address").Value:"",
                                                         duration = int.Parse(stop.Element("duration").Value),
                                                         location = new LocationInfo
                                                         {
                                                             latitude = double.Parse((stop.Element("location").Element("latitude")).Value),
                                                             longitude = double.Parse((stop.Element("location").Element("longitude")).Value)
                                                         },
                                                         pointsofInterest = (from pointofinterest in stop.Elements("pointOfInterest")
                                                                             select new PointOfInterest
                                                                             {
                                                                                 title = pointofinterest.Element("title").Value,
                                                                                 shortDescription = pointofinterest.Element("shortDescription").Value,
                                                                                 longDescription = pointofinterest.Element("longDescription").Value,
                                                                                 imageList = (from image in pointofinterest.Elements("image")
                                                                                              select new ImageInfo
                                                                                              {
                                                                                                  title = image.Attribute("title").Value,
                                                                                                  imageURI = image.Element("imageURI").Value,
                                                                                                  // This tag is optional
                                                                                                  imageCopyrightInfo = image.Element("imageCopyright") != null?image.Element("imageCopyright").Value:"" 
                                                                                              }).ToList<ImageInfo>(),
                                                                                 //This Tag is Optional
                                                                                 location = pointofinterest.Element("location") !=null? new LocationInfo
                                                                                 {
                                                                                     latitude = double.Parse((pointofinterest.Element("location").Element("latitude")).Value),
                                                                                     longitude = double.Parse((pointofinterest.Element("location").Element("longitude")).Value)
                                                                                 }:null,
                                                                                 poiTags = (from poiTags in pointofinterest.Elements("POITag")
                                                                                            select poiTags.Value).ToList<string>()
                                                                             }).ToList<PointOfInterest>()
                                                     }).ToList<TourStop>()
                                    }).ToList<Tour>();

                //Add all the tour list to the collection
                foreach (var item in tours)
                {
                    this.Items.Add(item);

                }
                this.isFileLoaded = true;
            }
            catch (Exception e)
            {

                Console.WriteLine(e);
            }

        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
     
    }
   
    public class Tour: INotifyPropertyChanged
    {
	    public string language;
	    public string id;
	    public string thumbnailURI;
	    public string title;
	    public string description;
	    public string author;
	    public List<TourStop> tourStops;
        public bool isVisited = false;

        #region properties

        public string Title
        {
            get
            {
                return title;
            }
        }

        public string ThumbNailURI
        {
            get
            {
                return thumbnailURI;
            }
           
        }

        public string Description
        {
            get
            {
                return description;
            }
        }

        public string Author
        {
            get
            {
                return author;
            }
        }



        #endregion properties

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    public class TourStop
    {
        public string id;
	    public string title;
	    public string description;
        public LocationInfo location;
	    public string address;
	    public int duration;
	    public List<PointOfInterest> pointsofInterest;
    }
    public class PointOfInterest
    {

	    public string title;
	    public string shortDescription;
	    public string longDescription;
	    public List<ImageInfo> imageList;
        public LocationInfo location;
	    public List<string> poiTags;

    }

    public class ImageInfo
    {
        public string title;
        public string imageURI;
        public string imageCopyrightInfo;

    }

    public class LocationInfo
    {
        public double latitude;
        public double longitude;
    }
}
